package com.zzk.lanouhn.myapplication1.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.FindDetailsXiaotuAdapter;
import com.zzk.lanouhn.myapplication1.adapters.FindDetailsXiaotuViewAdapter;
import com.zzk.lanouhn.myapplication1.fragments.FindDetailsXiaotuLike;
import com.zzk.lanouhn.myapplication1.fragments.FindDetailsXiaotuTravels;
import com.zzk.lanouhn.myapplication1.modles.FindDetailsXiaotuModle;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/10.
 */
public class FindDetailsXiaoTuActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;

    private Button btn_guanzhu, btn_sixing;

    private List<String> tabName = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private FindDetailsXiaotuViewAdapter viewAdapter;

    //作者信息设置
    private RecyclerView recyclerView;
    private FindDetailsXiaotuAdapter xiaotuAdapter;
    private FindDetailsXiaotuModle xiaotuModle;

    //接收到的值
    private String ID;
    private String detailsId;
    private String zhengID;

    //判断是否登录
    private SharedPreferences sp;
    private String login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_details_xiaotu);

        ID = getIntent().getStringExtra("xt_id");
        detailsId = getIntent().getStringExtra("finddetails_id");
        if (ID != null) {
            zhengID = ID;
        }
        if (detailsId != null) {
            zhengID = detailsId;
        }
        sp = getSharedPreferences("loginpic", MODE_PRIVATE);
        login = sp.getString("shifou", "false");

        initID();
        initDate();
        initEvent();
        getXiaoTuData();

        LinearLayoutManager llm = new LinearLayoutManager(FindDetailsXiaoTuActivity.this);
        recyclerView.setLayoutManager(llm);
    }

    private void initID() {
        tabLayout = (TabLayout) findViewById(R.id.find_details_xiaotu_tablayout);
        viewPager = (ViewPager) findViewById(R.id.find_details_xiaotu_viewPager);
        recyclerView = (RecyclerView) findViewById(R.id.find_details_xiaotu_recyclerView);
        btn_guanzhu = (Button) findViewById(R.id.find_details_xiaotu_guanzhu);
        btn_sixing = (Button) findViewById(R.id.find_details_xiaotu_sixing);
    }

    private void initDate() {
        tabName.add("游记");
        tabName.add("喜欢");

        FindDetailsXiaotuTravels flxt = new FindDetailsXiaotuTravels();
        FindDetailsXiaotuLike like = new FindDetailsXiaotuLike();

        fragments.add(flxt);
        fragments.add(like);
        //实例化适配器
        viewAdapter = new FindDetailsXiaotuViewAdapter(getSupportFragmentManager(), tabName, fragments);
        //设置适配器
        viewPager.setAdapter(viewAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void initEvent() {
        //关注的点击事件
        btn_guanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (login.equals("false")) {
                    Intent intent = new Intent(FindDetailsXiaoTuActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    if (btn_guanzhu.getText().equals("关_注_他")) {
                        Toast.makeText(FindDetailsXiaoTuActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
                        btn_guanzhu.setText("关 注 成 功");
                    } else {
                        Toast.makeText(FindDetailsXiaoTuActivity.this, "取消关注", Toast.LENGTH_SHORT).show();
                        btn_guanzhu.setText("关_注_他");
                    }
                }
            }
        });
    }

    private void getXiaoTuData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://chanyouji.com/api/users/" + zhengID + ".json?page=1";

                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String ss = response.body().string();
                        Gson gson = new Gson();

                        xiaotuModle = gson.fromJson(ss, FindDetailsXiaotuModle.class);

                        handler.sendEmptyMessage(1);

//                        try {
//                            JSONObject jsonArray = new JSONObject(ss);
//                            for (int i = 0; i < jsonArray.length(); i++) {
//                                FindDetailsXiaotuModle xtModle = gson.fromJson(jsonArray.get(i).toString(), FindDetailsXiaotuModle.class);
//                                xiaotuModle.add(xtModle);
//                            }
//
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }

                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    xiaotuAdapter = new FindDetailsXiaotuAdapter(FindDetailsXiaoTuActivity.this, xiaotuModle);
                    recyclerView.setAdapter(xiaotuAdapter);
                    break;
                case 0:
                    Toast.makeText(FindDetailsXiaoTuActivity.this, "失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

}
